package org.zh.client.tra_aop;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.zh.client.config.GlobalStatic.*;


@SuppressWarnings("all")
@Controller
public class GTCller {

    private final GlobalTransactionService globalTransactionService;

    public GTCller(GlobalTransactionService globalTransactionService) {
        this.globalTransactionService = globalTransactionService;
    }

    @RequestMapping(method = RequestMethod.PUT, path = GLOBAL_ROLLBACK_COMMIT, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public void rollback(@RequestBody RemoteTransactionBean rtb) {
        globalTransactionService.commitOrRollback(rtb.getGlobal_TX_ID(), rtb.isCommit());
    }

    @RequestMapping(method = RequestMethod.PUT, path = GLOBAL_REGISTRY, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public void registry(@RequestBody RemoteTransactionBean rtb) {
        globalTransactionService.putGlobalRegistry(rtb);
    }

    @RequestMapping(method = RequestMethod.PUT, path = GLOBAL_ERROR, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public void error(@RequestBody RemoteTransactionBean rtb) {
        globalTransactionService.putGlobalError(rtb);
    }
}
